package com.angel.web.app;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.base.JsonEntity;
import com.angel.core.util.JsonUtil;
import com.angel.health.BuzPres;
import com.angel.provider.app.BuzPresProvider;
import com.github.pagehelper.StringUtil;

/**
 * 高亚林
 * 2016年10月4日下午5:07:14
 */
@Controller
@RequestMapping("app/pres")
public class AppPresController {

	@Autowired
	private BuzPresProvider provider;
	
	//添加开处方记录
	@ResponseBody
	@RequestMapping(value = "addPres", produces = "application/json; charset=UTF-8")
	public String addPres(BuzPres pres){
		try {
			int i = provider.insert(pres);
			if (i > 0) {
				return JsonUtil.toJson(new JsonEntity("200", pres));
			} else {
				return JsonUtil.toJson(new JsonEntity("-1", "失败"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.toJson(new JsonEntity("-1", "失败"));
		}
	}
	
	//查询
	@ResponseBody
	@RequestMapping(value = "index", produces = "application/json; charset=UTF-8")
	public String index(BuzPres pres){
		if(StringUtil.isEmpty(pres.getCustomerId().toString()) && StringUtil.isEmpty(pres.getDoctorId().toString())){
			return JsonUtil.toJson(new JsonEntity("-1", "参数不正确！"));
		}
		List<Map<String,Object>> list = provider.findAll(pres);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (Map<String, Object> map : list) {
			map.put("visitTime", sdf.format(map.get("visitTime")));
		}
		return JsonUtil.toJson(new JsonEntity("200", list));
	}
}
